package com.xg.frame.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


@ApiModel(value = "全局统一返回结果", description = "全局统一返回结果")
public class R<T> {

    @ApiModelProperty(value = "是否成功 true 成功， false 失败")
    private boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

//    @ApiModelProperty(value = "返回数据")
//    private Map<String, Object> data = new HashMap<String, Object>();

    @ApiModelProperty(value = "返回对象数据")
    private T info;

    public T getInfo() {
        return info;
    }

    public void setInfo(T info) {
        this.info = info;
    }

    public R info(T value) {
        this.setInfo(value);
        return this;
    }

    /**
     * 成功
     *
     * @return
     */
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ConstantCode.OK.getCode());
        r.setMessage(ConstantCode.OK.getName());
        return r;
    }

    /**
     * 错误
     *
     * @return
     */
    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ConstantCode.ERROR.getCode());
        r.setMessage(ConstantCode.ERROR.getName());
        return r;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

//    public R data(String key, Object value) {
//        this.data.put(key, value);
//        return this;
//    }
//
//    public R data(Map<String, Object> map) {
//        this.setData(map);
//        return this;
//    }

    /**
     * 无权限
     *
     * @return
     */
    public static R noAccess() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(401);
        r.setMessage("invalid token");
        return r;
    }

    /**
     * 数据校验
     *
     * @return
     */
    public static R noVerify() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ConstantCode.NOT_VERIFY.getCode());
        r.setMessage(ConstantCode.NOT_VERIFY.getName());
        return r;
    }


    /**
     * 接口异常
     *
     * @return
     */
    public static R netError() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ConstantCode.NET_ERROR.getCode());
        r.setMessage(ConstantCode.NET_ERROR.getName());
        return r;
    }


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
